import os
import json
from modelscope.trainers import build_trainer
from modelscope.msdatasets import MsDataset
from modelscope.utils.hub import read_config
from modelscope.metainfo import Metrics
from modelscope.utils.constant import DownloadMode


model_id = 'iic/nlp_structbert_siamese-uninlu_chinese-base'

WORK_DIR = '/tmp'

train_dataset = MsDataset.load('damo/people_daily_ner_1998_tiny', namespace='damo', split='train', download_mode=DownloadMode.FORCE_REDOWNLOAD)
eval_dataset = MsDataset.load('damo/people_daily_ner_1998_tiny', namespace='damo', split='validation', download_mode=DownloadMode.FORCE_REDOWNLOAD)


max_epochs=3
kwargs = dict(
    model=model_id,
    model_revision='master',
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    max_epochs=max_epochs,
    work_dir=WORK_DIR)


trainer = build_trainer('siamese-uie-trainer', default_args=kwargs)

print('===============================================================')
print('pre-trained model loaded, training started:')
print('===============================================================')

trainer.train()

print('===============================================================')
print('train success.')
print('===============================================================')

for i in range(max_epochs):
    eval_results = trainer.evaluate(f'{WORK_DIR}/epoch_{i+1}.pth')
    print(f'epoch {i} evaluation result:')
    print(eval_results)


print('===============================================================')
print('evaluate success')
print('===============================================================')
